package com.gl.order.controller;

import com.gl.order.commom.ResultBean;
import com.gl.order.entity.dto.SysDictAddDto;
import com.gl.order.entity.dto.SysDictEditDto;
import com.gl.order.service.SystemDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: liangSY
 * @Date: 2021/6/23 8:47
 * @ClassName: 系统字典管理处
 */
@Api(tags = "系统字典表处理")
@RestController
@RequestMapping("/sys/dict")
public class SysDictController {

    SystemDictService dictService;

    public SysDictController(SystemDictService dictService) {
        this.dictService = dictService;
    }

    @GetMapping("/findByTypeNot")
    @ApiOperation("获取数据字典所有数据")
    public ResultBean<List> findByTypeNot(@RequestParam("type") String type){
        return ResultBean.success(dictService.findByTypeNot(type));
    }

    @GetMapping("/findByType")
    @ApiOperation("获取数据字典")
    public ResultBean<List<SysDictAddDto>> findByType(@RequestParam("type") String type){
        return ResultBean.success(dictService.findByType(type));
    }

    @GetMapping("/findByMenuPath")
    @ApiOperation("根据menuPath获取数据字典")
    public ResultBean findByMenuPath(@RequestParam("menuPath") String menuPath){
        return ResultBean.success(dictService.findByMenuPath(menuPath));
    }

    // 添加字典数据
    @PostMapping("/add")
    @ApiOperation("添加字典数据")
    public ResultBean addSysDict(@RequestBody @Valid SysDictAddDto dto){
        dictService.addSysDict(dto);
        String notType = dto.getNotType();
        return ResultBean.success(dictService.findByTypeNot(notType));
    }

    @PutMapping("/edit")
    @ApiOperation("修改字典数据")
    public ResultBean updateSysDict(@RequestBody @Valid SysDictEditDto dto){
        dictService.updateByDto(dto);
        return ResultBean.success();
    }

    @DeleteMapping("/del")
    @ApiOperation("删除数据字典")
    @ApiImplicitParam(name = "id", value = "字典id", dataType = "String", paramType = "path")
    public ResultBean delSysDict(@RequestParam("id") String id, @RequestParam("type") String type){
        if(dictService.removeById(id)){
            List dict = dictService.findByTypeNot(type);
            return ResultBean.success(dict);
        }
        return ResultBean.failed("删除失败");
    }

}
